import { App } from 'vue'
import { useUserInfo } from '../stores/userInfo'
import pinia from '../stores'
import { Permission } from '../constants'

export default {
  install: (app: App) => {
    app.config.globalProperties.$can = (key: Permission): boolean => {
      const { userInfos } = useUserInfo(pinia)
      const permissions = userInfos?.permissions || []
      return userInfos.super_admin || permissions.includes(key)
    }

    app.config.globalProperties.$cannot = (key: Permission): boolean => {
      const { userInfos } = useUserInfo(pinia)
      const permissions = userInfos?.permissions || []
      return userInfos.super_admin || !permissions.includes(key)
    }
  }
}
